package com4280;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
 * This is the GUI for the spider. You can enter a base URL which the spider will start on and the spider will not deviate
 * from this server.
 *
 * @author Andrew Evans
 * @author Mitchell Whitehouse
 * @author Duncan Grant
 * @version 1.0
 */
public class MainScreen extends JFrame
{
	private JTextField url;
	private JButton start;
	private JButton stop;
	private JButton resume;
	private JButton kill;
	private JTextPane output;
	private MADBot caller;

    /**
     *
     * @param caller the running MADBot object.
     */
	public MainScreen(MADBot caller)
	{
		this.caller = caller;
		setup();
	}

    /**
     *
     * @return the starting (base) URL
     */
	public String getStartingURL()
	{
		return url.getText();
	}

    /**
     * Appends a String to the end of the output box.
     * @param toOutput a String to output on a new line in the GUI
     */
	public void addOutput(String toOutput)
	{
		String text = output.getText();
		output.setText(text + "\n" + toOutput);
	}

    /**
     * Sets up the GUI and displays it to the user.
     */
	private void setup()
	{
		setSize(500,300);
		setLocationRelativeTo(null);
		setTitle("MADBOT Command Centre");
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		initButtons();
		
		Container mainPanel = getContentPane();
		mainPanel.setLayout(new BorderLayout());
		
		JPanel topPanel = new JPanel();
		topPanel.setLayout(new GridLayout(2,1));
		
		JPanel urlPanel = new JPanel();
		urlPanel.add(new JLabel("Enter URL:"));
		url = new JTextField(20);
        // Default URL is the DCS testing server as this is most commonly used for this project.
		url.setText("http://poplar.dcs.shef.ac.uk/~u0082/intelweb2/");
		urlPanel.add(url);		
		
		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new GridLayout(1,4));
		JPanel startPanel = new JPanel();
		startPanel.add(start);
		JPanel stopPanel = new JPanel();
		stopPanel.add(stop);
		JPanel resumePanel = new JPanel();
		resumePanel.add(resume);
		JPanel killPanel = new JPanel();
		killPanel.add(kill);
		
		buttonPanel.add(startPanel);
		buttonPanel.add(stopPanel);
		buttonPanel.add(resumePanel);
		buttonPanel.add(killPanel);
		
		output = new JTextPane();
		
		topPanel.add(urlPanel);
		topPanel.add(buttonPanel);
		
		mainPanel.add(topPanel, BorderLayout.NORTH);
		mainPanel.add(new JScrollPane(output), BorderLayout.CENTER);
		
		setVisible(true);
	}

    /**
     * Initializes the buttons and gives each button an action listener.
     */
	private void initButtons()
	{
		/*
		Starts the spider with the specified input URL.
		 */
        start = new JButton("Start");
		start.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				output.setText("");
				caller.startIWSpider(url.getText());
			}
		});

        /*
        Pauses the spider. The program can be resumed from the current position at a later time.
         */
		stop = new JButton("Pause");
		stop.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				caller.stopIWSpider();
			}
		});

        /*
        Resumes the spider from the latest position.
         */
		resume = new JButton("Resume");
		resume.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				caller.resumeIWSpider();
			}
		});

        /*
        Kills the spider (completely stops process). Spider will have to be restarted after this.
         */
		kill = new JButton("Kill");
		kill.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				caller.killIWSpider();
				
			}
		});
	}
}
